package cn.tedu.weibo02.controller;

import cn.tedu.weibo02.mapper.WeiboMapper;
import cn.tedu.weibo02.pojo.dto.WeiboDTO;
import cn.tedu.weibo02.pojo.entity.Weibo;
import cn.tedu.weibo02.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/weibo/")
public class WeiboController {
    @Autowired(required = false)
    WeiboMapper mapper;
    @RequestMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return 2;//代表未登录
        }
        Weibo w = new Weibo();
        BeanUtils.copyProperties(weiboDTO,w);
        w.setCreated(new Date());
        w.setUserId(userVO.getId());
        mapper.insert(w);
        return 1;
    }
}
